<?php

namespace App\Models\Traits;

use App\Models\HintStatus;
use App\Models\UnPayHintStatus;

trait HasHint
{
    public function hint()
    {
        return $this->morphOne(HintStatus::class, 'hintable');
    }

    /**
     * 获取状态对象
     * @return HintStatus
     */
    public function getHint()
    {
        return $this->hint()->firstOrCreate([
            'hintable_id' => (string)$this->id,
            'hintable_type' => static::class,
        ], [
            'class' => UnPayHintStatus::class,
            'comment' => ''
        ])->status();
    }

    /**
     * 获取状态说明
     *
     * @return mixed
     */
    public function getStatusAttribute()
    {
        if (!$this->id) {
            return null;
        }

        return $this->getHint()->toString();
    }

    public function getStatusCodeAttribute()
    {
        return HintStatus::STATUS_CODE[$this->hint->class];
    }

    public function getStatusCommentAttribute()
    {
        return $this->hint->comment;
    }
}
